package com.lianjinsu.shop.repository.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lianjinsu.shop.core.mybatis.AdvancedDaoImpl;
import com.lianjinsu.shop.entity.Shop;
import com.lianjinsu.shop.repository.dao.ShopDao;
import com.lianjinsu.shop.repository.mapper.ShopMapper;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Repository
public class ShopDaoImpl extends AdvancedDaoImpl<ShopMapper, Shop> implements ShopDao {

    @Override
    public boolean increaseStock(int id, int stock) {
        Shop shop = getById(id);
        shop.setStock(shop.getStock() + stock);
        return updateById(shop);
    }

    @Override
    public boolean hotOrNot(int id, boolean hot) {
        Shop shop = new Shop();
        shop.setShopId(id);
        shop.setHot(hot);
        return updateById(shop);
    }

    @Override
    public List<Shop> selectByIds(Collection<Integer> ids) {
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery();
        wrapper.in(Shop::getShopId, ids);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Shop> selectByType(String type) {
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Shop::getTypeId, type);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Shop> selectByTags(Collection<String> tags) {
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtils.isNotEmpty(tags), Shop::getTag, tags);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Shop> hotList() {
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Shop::getHot, true);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Shop> selectListName(String shopName) {
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Shop::getShopName, shopName);
        return baseMapper.selectList(wrapper);
    }
}
